<?php

require_once 'libs/mysql.class.php';
require_once 'config/table.conf.inc';

class TableManager
{
	private $conn;
	
	public function __construct( $tableName )
	{
		$this->conn = new Mysql();
	}
	
	public function getStructureTable( $tableName )
	{
		$conn           = $this->conn;
		$columnNames    = $conn->GetColumnNames($tableName);
		$columnComments = $conn->GetColumnComments($tableName);
        $pKeys          = $this->getPrimaryKeys($tableName);
		
		$datas = array();
		foreach($columnNames as $columnName) {
			$datas[] = array(
					'fieldName' => $columnName,
					'label'     => ucfirst($this->_getLabel($columnName, $columnComment,  $tableName)),
					'type'      => $conn->GetColumnDataType($columnName, $tableName),
                    'value'     => '',
                    'primary'   => in_array($columnName, $pKeys) ? true : false,
			);
		}
		return $datas;
	}
	
	public function getDatas( $tableName, $sqlQuery )
	{
		$conn           = $this->conn;
		$columnComments = $conn->GetColumnComments($tableName);
		$items          = $conn->QueryArray($sqlQuery, MYSQL_ASSOC);
		$pKeys          = $this->getPrimaryKeys($tableName);
		
        $datas = array();
		foreach ($items as $values) {
    		foreach ($values as $columnName=>$value) {
    		    $columnComment = $columnComments[$columnName];
    		    
    			$datas[] = array(
    				'fieldName' => $columnName,
    			    'label'     => ucfirst($this->_getLabel($columnName, $columnComment,  $tableName)),
    				'type'      => $conn->GetColumnDataType($columnName, $tableName),
    				'value'     => $value,
    			    'primary'   => in_array($columnName, $pKeys) ? true : false,
    			);
    		}
		}
		return $datas;
	}
	
	public function insert( $tableName )
	{
		$conn           = $this->conn;
		$columnNames    = $this->getStructureTable( $tableName );
		
		foreach($columnNames as $columnName) {
			$valuesArray[$columnName] = $_REQUEST[$columnName];
		}
		
		$conn->InsertRow($tableName, $valuesArray);
	}
	
	public function update($tableName, $whereArray)
	{
		$conn           = $this->conn;
		$columnNames    = $this->getStructureTable( $tableName );

		foreach($columnNames as $columnName) {
			$valuesArray[$columnName['fieldName']] = "'".addslashes($_REQUEST[$columnName['fieldName']])."'";
		}
		return $conn->UpdateRows($tableName, $valuesArray, $whereArray);

	}
	
	public function delete($tableName, $whereArray)
	{
		$conn           = $this->conn;
		$conn->DeleteRows($tableName, $whereArray);
	}
	
	private function _replaceNotEmpty( $string )
	{
		return (isset($string) && $string != '') ? utf8_encode($string) : '';
	}
	
	private function _getLabel($columnName, $columnComment, $tableName)
	{
	    if ( $columnComment != '' ) {
	        return utf8_encode($columnComment);
	    }
	    else if ( isset($GLOBALS['columnMapping'][$tableName]) && array_key_exists($columnName, $GLOBALS['columnMapping'][$tableName]) ) {
	        return utf8_encode($GLOBALS['columnMapping'][$tableName][$columnName]);
	    }
	    else {
	        return utf8_encode($columnName);
	    }
	}
    
    public function getPrimaryKeys($tableName)
    {
        $conn = $this->conn;
        
        // Recuperation du nom du champ de la cle primaire
		$keys  = $conn->QueryArray("SHOW KEYS FROM $tableName WHERE Key_name = 'PRIMARY'", MYSQL_ASSOC);
		$pKeys = array();
		foreach( $keys as $key ) {
		    $pKeys[] = $key['Column_name'];
		}
        return $pKeys;
    }
    
    
}